<?php
//defined('BASEPATH') OR exit('No direct script access allowed');
namespace lib\wx;
include_once("jiaweixs/weapp/src/WeApp.php");
include_once("jiaweixs/simplecache/src/SimpleCache.php");
use JiaweiXS\WeApp\WeApp;
  
class Liteapp {
    private $appid;
    private $secret;
    private $cache;
    private $weapp;


    public function __construct()
    {
       // echo $_SERVER['DOCUMENT_ROOT'];exit;

       
       $this->appid = config('weapp_appid');
       $this->secret= config('weapp_secret');
       $this->cache = config('weapp_cache');

       $this->weapp = new WeApp($this->appid,$this->secret,$this->cache);

    }
    //登陆获取sessionkey等信息
    public function getSessionkey($code)
    {        
        $result =$this->weapp->getSessionKey($code);
        return json_decode($result,TRUE);
    }

    //解密工具
    public function decode($sessionKey,$encryptedData,$iv)
    {
        include_once("WXBizDataCrypt/wxBizDataCrypt.php");
        
        $pc = new WXBizDataCrypt($this->appid, $sessionKey);
        $errCode = $pc->decryptData($encryptedData, $iv, $data );

        if ($errCode == 0) {
            return $data;
        } else {            
            return $errCode;
        }
    }

}